package com.javaking.clanteam.studentutils.courses;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.javaking.clanteam.studentutils.R;
import com.javaking.clanteam.studentutils.sql.CourseHelper;


public class CourseListFragment extends SherlockListFragment {
	
	List<Map<String,String>> mCourseList;
	
	Context mContext;
	
	private SimpleAdapter mAdapter;
	
	/**
	 * @see android.app.Fragment#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mContext = getActivity();
		
		mCourseList = new ArrayList<Map<String,String>>();
		
		mAdapter = new SimpleAdapter(mContext, mCourseList,
				R.layout.fragment_courselist_listitem,
				new String[] {CourseHelper.COLUMN_TEACHER,
					CourseHelper.COLUMN_COURSE_TITLE,
					CourseHelper.COLUMN_ROOM},
				new int[] {R.id.teacher,R.id.courseName,R.id.room});
		
		updateCourseList();
		
		setListAdapter(mAdapter);
		
		
		/* For testing purposes only */
//		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1);
//		arrayAdapter.add("Item 1");
//		arrayAdapter.add("Item 2");
//		setListAdapter(arrayAdapter);
		
		setHasOptionsMenu(true);
	}

	public void updateCourseList() {
		CourseHelper helper = new CourseHelper(mContext);
		List<CourseData> courses = Arrays.asList(helper.getAllCourses());
		helper.close();
		
		mCourseList.clear();
		
		for (CourseData course : courses) {
			mCourseList.add(course.getCopyOfMap());
		}
		
		mAdapter.notifyDataSetChanged();
	}
	
	/**
	 * @see android.app.Fragment#onCreateOptionsMenu(android.view.Menu, android.view.MenuInflater)
	 */
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.fragment_courselist, menu);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return LayoutInflater.from(mContext).inflate(R.layout.fragment_courselist,
				container, false);
	}
	
	/**
	 * @see android.app.Fragment#onResume()
	 */
	@Override
	@SuppressLint("NewAPI")
	public void onResume() {
		super.onResume();
		updateCourseList();
		
		if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.HONEYCOMB) {
			ActionBar bar = getActivity().getActionBar();
			if (bar!=null) {
				bar.setTitle(R.string.title_activity_courses);
			}
		}
	}
	
	/**
	 * @see android.app.Fragment#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Fragment frag = null;
		switch (item.getItemId()) {
		case R.id.addCourse:
			frag = new AddCourseFragment();
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		
		if (frag instanceof DialogFragment) {
			((DialogFragment)frag).show(getFragmentManager(), null);
		} else {
			// The support library is being dumb and replace isn't actually removing
			// the old fragment
			getFragmentManager().beginTransaction()
				.remove(this).add(getId(), frag).addToBackStack(null).commit();
		}
		return true;
		
	}
	
	/**
	 * @see android.app.ListFragment#onListItemClick(android.widget.ListView, android.view.View, int, long)
	 */
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		CourseData data = CourseData.buildCourseFromMap(mCourseList.get(position));
		
		Bundle args = new Bundle();
		args.putParcelable("course", data);
		
		Intent intent = new Intent(mContext, CourseManagementActivity.class);
		intent.putExtras(args);
		
		startActivity(intent);
	}
}
